# conftest.py
import pytest
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as SV
from Tools.commos.extract import extract

@pytest.fixture(scope="session")
def browser():
    # 初始化环境
    chrome_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'

    chrome_service = SV(executable_path=chrome_path)


    # 配置浏览器选项
    # options = Options()
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

    # chrome_options.add_argument("--headless")  # 无头模式，不显示GUI
    # chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--window-size=1920,1080")

    # 初始化浏览器
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
    driver.maximize_window()

    driver.implicitly_wait(100)  # 隐式等待

    yield driver

    # 测试结束后清理
    driver.quit()

@pytest.fixture(scope='class')
def login():
    url = "https://openapiv5.ketangpai.com//UserApi/login"
    data = '{"email":"790874262@qq.com","password":"3uMxtZNNJDRBl/yGgb+Anw==","remember":"0","code":"","mobile":"","type":"login","encryption":1,"reqtimestamp":1753602819746}'
    response = requests.post(url, data=data)
    print(f"登录成功信息：{response.json()}")
    obj_token=response.json()
    get_token = extract(obj_token,'$.data.token')[0]
    print(type(get_token))
    print(f"这是get_token{get_token}")
    assert response.status_code == 200
    # 返回 token
    # return response.json().get("token")
    return get_token
# test_example.py
# def selenium(browser):
#     d=webdriver.Chrome()
#     yield d
#     d.quit()
# @pytest.fixture()
# def selenium():
#     d = webdriver.Chrome()
#     yield d
#     d.quit()


    # browser.get("https://www.chiphell.com/portal.php")
    # s = r'首页 -  Chiphell - 分享与交流用户体验   '
    # assert s in browser.title
#
#
# def test_homepage(browser):
#     browser.get("https://example.com")
#     assert "Example" in browser.title
